<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="130px">
    <el-form-item label="用户昵称" prop="nickName">
      <el-input v-model="user.nickName" maxlength="30" />
    </el-form-item>
    <el-form-item label="手机号码" prop="phonenumber">
      <el-input v-model="user.phonenumber" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="user.email" maxlength="50" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">男</el-radio>
        <el-radio label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="身份证号">
      <el-input v-model="user.idCard"/>
    </el-form-item>
    <el-form-item label="出生日期">
      <el-date-picker
        v-model="user.birthDate"
        type="date"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="所属专业">
      <TheDeptSelect v-model="user.majorId" :modelValue="user.majorId" deptType="40" placeholder="请选择所属专业" @update:modelValue="user.majorId = $event"/>
    </el-form-item>
    <el-form-item label="职称">
      <el-input v-model="user.title"/>
    </el-form-item>
    <el-form-item label="获得职业证书名称">
      <el-input v-model="user.certificateName"/>
    </el-form-item>
    <el-form-item label="获得职业证书时间">
      <el-date-picker
        v-model="user.certificateDate"
        type="date"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="证书类型">
      <el-select v-model="user.certificateTypeId" placeholder="证书类型">
        <el-option
          v-for="(dictItem,index) in certificate_type"
          :label="dictItem.dictLabel"
          :value="dictItem.dictCode"
          :key="index"
          ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="学历">
      <el-select v-model="user.educationId" placeholder="证书类型">
        <el-option
          v-for="(dictItem,index) in education_name"
          :label="dictItem.dictLabel"
          :value="dictItem.dictCode"
          :key="index"
          ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="简历信息">
    <el-button type="primary" @click="addResumeItem">新增</el-button>
    <el-table :data="user.staffExperiencesList" style="width: 100%; margin-top: 10px;">
      <el-table-column label="开始时间">
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.startDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间"
          ></el-date-picker>
        </template>
      </el-table-column>
      <el-table-column label="结束时间">
        <template slot-scope="scope">
          <el-date-picker
            v-model="scope.row.endDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间"
          ></el-date-picker>
        </template>
      </el-table-column>
      <el-table-column label="单位名称">
        <template slot-scope="scope">
          <el-input v-model="scope.row.organization" maxlength="30" />
        </template>
      </el-table-column>
      <el-table-column label="专业方向/职务">
        <template slot-scope="scope">
          <el-input v-model="scope.row.position" maxlength="30" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="deleteResumeItem(scope.$index)"
            style="color: #f56c6c;"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";
import { useDict } from '@/utils/dict/useDict'
import TheDeptSelect from '@/componentsView/TheDeptSelect.vue';
export default {
  components: { TheDeptSelect },
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      tableData:[],
      certificate_type: useDict('certificate_type'),
      education_name: useDict('education_name'),
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.user).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
    // 新增简历项
    addResumeItem() {
      this.user.staffExperiencesList.push({
        startDate: '',
        endDate: '',
        organization: '',
        position: ''
      });
    },
    // 删除简历项
    deleteResumeItem(index) {
      this.$confirm('确定要删除这条记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData.splice(index, 1);
        this.$message.success('删除成功');
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
  }
};
</script>
